package cn.fayang.org.service.impl;

import cn.fayang.basic.exception.BusinessException;
import cn.fayang.basic.util.BaiduAuditUtils;
import cn.fayang.org.domain.Employee;
import cn.fayang.org.domain.Shop;
import cn.fayang.org.domain.ShopAuditLog;
import cn.fayang.org.mapper.EmployeeMapper;
import cn.fayang.org.mapper.ShopAuditLogMapper;
import cn.fayang.org.mapper.ShopMapper;
import cn.fayang.org.service.IShopService;
import cn.fayang.basic.service.impl.BaseServiceImpl;
import cn.fayang.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fayang
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;


    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //判断输入内容是否为空
        if(StringUtils.isEmpty(shop.getName())
        || StringUtils.isEmpty(admin.getUsername())
        || StringUtils.isEmpty(shop.getAddress())
        ){
            throw new BusinessException("必选内容不能为空");
        }
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名含有不合法字符");
        }



        //判断店铺是否被注册
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if(shopTmp != null){
            throw new BusinessException("店铺已经被注册");
        }
        //判断店铺管理员是否被注册
        Employee employeeTmp = employeeMapper.loadByUserName(admin.getUsername());
        if(employeeTmp != null && employeeTmp.getShop_id() != null){
            throw new BusinessException("该管理员已有注册店铺");
        }
        if(employeeTmp == null){
            employeeMapper.save(admin);//要返回自增ID
            employeeTmp = admin;
        }
        //  empTmp 一定有值
//        *  四：保存数据
//        *    shop
        shop.setAdminId(employeeTmp.getId());
        shopMapper.save(shop);//返回自增ID
//        *    employee  shopId
//        *    反过来修改shop中的admin_id
        employeeTmp.setShop_id(shop.getId());
        employeeMapper.update(employeeTmp);
    }

    @Override
    public void pass(ShopAuditLog log) {
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(1);
        shopMapper.update(shop);
        log.setState(shop.getState());
        log.setAuditId(333L);
        shopAuditLogMapper.save(log);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1786369411@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物乐园-店铺入驻激活请求");
        //邮件内容
        mailMessage.setText("亲，您的店铺信息已经通过审核，请尽快完成激活并使用：" +
                "激活链接：http://localhost:8080/shop/activation/"+log.getShopId());
        //收件人
        mailMessage.setTo("1786369411@qq.com");

        javaMailSender.send(mailMessage);
    }

    @Override
    public void rejec(ShopAuditLog log) {
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(-1);
        shopMapper.update(shop);
        log.setAuditId(333L);
        shopAuditLogMapper.save(log);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1786369411@qq.com");
        //邮件主题
        mailMessage.setSubject("宠物乐园-店铺入驻驳回任务");
        //邮件内容
        mailMessage.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：" +
                "http://localhost:8081/#/register2?shopId="+log.getShopId());
        //收件人
        mailMessage.setTo("1786369411@qq.com");

        javaMailSender.send(mailMessage);
    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }

    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }
}
